<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-form-item label="益联券" prop="voucherAmount">
          <el-input type="number" v-model="form.voucherAmount" placeholder="益联券金额" maxlength="6"
                    class="col"></el-input>
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input type="number" v-model="form.integral" placeholder="积分数值" maxlength="6" class="col"></el-input>
        </el-form-item>
      </el-tabs>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:business:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="发放类型:">
          <el-select v-model="listPram.integralDistributionType" placeholder="请选择" style="width: 200px;">
            <el-option
                v-for="item in statusList"
                :key="item.id"
                :label="item.data"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间：">
          <el-date-picker
              v-model="value1"
              @change="onchangeTime"
              value-format="yyyy-MM-dd" format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 300px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}"
                border size="mini">
        <el-table-column label="发放时间" min-width="130" prop="distributionTime"/>
        <el-table-column label="益联券" min-width="100" prop="amount"/>
        <el-table-column label="益积分" min-width="80" prop="integral"/>
        <el-table-column label="发放类型" min-width="80" prop="integralDistributionTypeName"/>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          :total="listData.count"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>
<script>
import {businessGrant, detail, distributionList} from "@/api/business";
import {getForm} from "@/utils/utils";

export default {
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      value1:'',
      listData: {list: [], count: 0},
      listPram: {
        page: 1,
        limit: 20,
        business: '',
        dateLimit: '',
        integralDistributionType: '',
      },
      activeName: "first",
      otherInfoVisible: false,
      form: {
        id: null,
        voucherAmount: 0,
        integral: 0,
      },
      rules: {},
      roles: [],
      saveLoading: false,
      formLoading: false,
      expressCompanyList: [],
      statusList: [{id: 1, data: '发放'}, {id: 2, data: '扣减'}],
    }
  },
  mounted() {
    if (this.id) {
      this.getDetail();
    }
    this.getList()
  },
  methods: {
    onchangeTime(e) {
      this.timeVal = e;
      this.listPram.dateLimit = e ? this.timeVal.join(',') : '';
    },
    getList() {
      this.listPram.business = this.id
      distributionList(this.listPram).then(res => {
        this.listData = res;
      }).finally(() => {
      })
    },
    handleClick(tab) {
      if (tab.name === 'second') {
        this.otherInfoVisible = true;
      } else {
        this.otherInfoVisible = false;
      }
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.status = 2;
        businessGrant(this.id, this.form.voucherAmount, this.form.integral).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideGrantDialog')
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    resetQuery() {
      this.listPram.integralDistributionType = null;
      this.listPram.dateLimit = '';
      this.value1 = '';
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
.col {
  width: 360px;
}

.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;

  .img {
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
